home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Reference Guide
/
C-C++ Interactive Reference Guide.iso
/
c_ref
/
csource4
/
274_01
/
array.doc
< prev
next >
Wrap
Text File
|
1979-12-31
|
13KB
|
377 lines
Array Handling Package
(C) Copyright 1985,1987,1988 James P. Cruse - All Rights Reserved
Array.doc
Version 1.00
This set of C header files is used to simplify array handling in C.
Included are routines to add, subtract, fill, negate, and get info on
arrays. See Complete Descriptions of functions below.
First, the Legal Issues:
The routines are copyrighted by the author, and have been released as a
user supported program. You may use this program on a trial basis, and if
you find it to be useful, or you continue using it, you must register it.
Any Commercial enterprises using this software must register it immediately.
You may not include the Source code of these routines with any
commercial package without written consent of the author. You may use
them to develop commercial programs upon registration.
Simple registration of these routines is $10.00. If you wish to recieve
an upgrade of the routines, and trial usage of the STAT and function
equivalents, the registration fee is $25.00. To recieve a copy of the
Scientific Functions is $50.00. (See Description below).
Registration should be mailed to:
James P. Cruse
919 Capitola Avenue
Suite 41
Capitola, CA 95010
(408) 475-7444 (m)
I am currently developing other routines for C, as well as improving
these. Routines that I am developing include: integer, float, and
"user" defined functions (not macros) that are equivalent to these routines;
Simple Statistical functions (mean,median,mode,r^2,s^2,standard deviation);
Scientific Functions (polynomials, curve fits, convolutions); and
Plotting routines for the IBM PC/XT/AT and compatibles (routines to plot on
screen, printer or HP plotter x-y plots of up to 16 parameters in a 4
windows).
Back to the routines:
The routines are broken up into the following types:
Binary Operations Add, Subtract, Multiply, etc. between 2 arrays
Unary Operations Negate, Assign, absolute, etc. an array
Fill Operations Fill array with index, index+off, values between x&y
Info about Array Min, Max, etc of an array
There are also routines to call a function on 1 or 2 arrays.
The routines work by substituting your call into a while loop. This was the
ONLY way I could find to use different types of arrays/constants in an
expression that would be compatible across a wide range of compilers/systems.
(I spent 3 months trying all kinds of different ways, but none would
work on ALL of the compilers I could test with, except this method).
The Functions all have a uniform naming format:
X_NAME, where X is a, for single array operations, aa for
2 array, ac for array & constant, and ca for contant & array
And NAME is the operation to be done. For Example:
aa_add() add two arrays
ac_add() add array & constant
a_neg() negate array
a_index() fill array with index
They also have a uniform parameter format:
X_NAME(s,d,...) where s is length of array, d is destination array, and
"..." varies for each type of operation,
a,b are arrays, c is a constant:
Binary operations have 2 parameters, a and b or a and c
Unary operations have a single parameter a
info operations have a single parameter
and fill operations take multiple parameters.
The function can also take other parameters, depending on the
operation.
See the file array.h for customization options. In order to use the
functions, place all the .h files in your include directory, and
include the file "array.h". This will load in the files you have
defaulted to be loaded. See array.h for further details.
I also include 3 Test Files, a_testall.c, a_testf.c and a_testi.c.
They test the various functions. a_testf does a floating point test,
a_testi does an integer test, and a_testall.c tests All of the routines.
NOTE: due to roundoff errors, and floating compare for equality, the
a_testall may not compare properly for Floats or Doubles.
I recommend looking at a_testi or a_testf for examples on how to code
using these algorithms, as a_testall is a large testing program with
little basis in reality.
The routines have been tested with multiple compilers including Microsoft
5.0, Turbo 1.5 and Aztec 3.40B all on a PC Clone.
The following is a quick description of the operations available.
Remember to prepend the a_,aa_,ac_,ca_ to the name depending on
the parameters to the operation. Each list is followed by an example
of a couple of the functions. For more information, see the list
at the end of the file to get a list of Every function.
Binary Operations: (a_binop.h)
add, sub, mul, div, min, max, mod (%,modulo),
equ (==), geq (>=), leq (<=), gtr (>), les (<),
fun (function), t_fun (typed function)
aa_add(n,d,a,b) d[] = a[] + b[]
ac_min(n,d,a,c) d[] = ARR_MIN(a[],c)
ca_sub(n,d,c,a) d[] = c - a[]
aa_fun(n,d,a,b,f) d[] = f(a[],b[])
ac_t_fun(n,d,a,c,f,t) d[] = f( (t) a[] , (t) c )
Unary Operations: (a_unop.h)
cpy (copy,assign), neg (negate), abs (absolute value),
rsum (running sum), rprod (running product),
rmin (running min), rmax (running max),
fun (function), t_function (typed function)
scale (scale&offset)
a_cpy(n,d,a) d[] = a[]
a_rsum d[i] = d[i-1] + a[i]
a_rmax d[i] = ARR_MAX( d[i-1] , a[i] )
a_scale(n,d,a,s,o) d[] = o + s*a[]
a_fun(n,d,a,f) d[] = f(a[]])
a_t_fun(n,d,a,f,t) d[] = (t) f( (t) a[] )
Fill Operations: (a_fill.h)
NOTE: please see the discussion in a_fill.h for details concerning
the different versions of fill.
index, indoff (index+offset), fill (exclusive),
ifill (inclusive), t_fill (typed fill), t_ifill (typed inc. fill)
scale (scale&offset),
assign, f_fun (function), i_fun, (function w/index param),
t_i_fun (typed function w/index param)
a_indoff(n,d,c) d[i] = i + c
a_assign(n,d,c) d[] = c
a_i_scale(n,d,s,o) d[i] = s*i + o
a_fill(n,d,s,e) d[0] = s, d[i] = s+(e-s)/n, d[n] (would be) e
a_t_ifill(n,d,s,e) d[0] = s, d[i] = s+((t)(e-s))/(n-1), d[n-1] = e
a_f_fun(n,d,f) d[] = f()
a_t_i_fun(n,d,f,t) d[i] = f( (t) i )
Info Operations: (a_info.h)
sum, prod, min, max, minmax
a_sum(n,d,a) d = a[0] + a[1] + ... + a[n-1]
a_min(n,d,a) d = min( a[0] , a[1] , ... , a[n-1] )
a_minmax(n,l,u,a) l = min( a[0] , a[1] , ... , a[n-1] ),
and u = max( a[0] , a[1] , ... , a[n-1] )
COMPLETE SUMMARY LIST OF FUNCTIONS:
Notation:
d => destination parameter d - a constant
d[] => all elements of array d (from 0 to n-1)
d[i] => the i'th element of array d (from 0 to n-1)
d[i-1] => the i-1'th element of array d (from 1 to n-1)
a[+] => all elements of a[] are "+"'ed (sum of a[])
s => parameter s
f() => function parameter f (i.e. cos)
T => function type T (i.e. float)
running "sums" are d[0] = a[0], d[1] = d[0] "+" a[1], ...
d[n-2] = d[n-3] "+" a[n-2], d[n-1] = d[n-2] "+" a[n-1]
BINARY OPERATIONS:
Note:
for each operation, there are 3 macros, for each combination
of arrays and constants, for example:
aa_op(n,d,a,b) array, array operation (d[] = a[] + b[])
ac_op(n,d,a,c) array, constant operation (d[] = a[] * c)
ca_op(n,d,c,a) constant, array operation (d[] = c - a[])
The functions are:
aa_add(n,d,a,b) d[] = a[] + b[] add a[] and b[]
ac_add(n,d,a,c) d[] = a[] + c add a[] and c
ca_add(n,d,c,b) d[] = c + b[] add c and b[]
aa_sub(n,d,a,b) d[] = a[] - b[] sub b[] from a[]
ac_sub(n,d,a,c) d[] = a[] - c sub c from b[]
ca_sub(n,d,c,b) d[] = c - b[] sub b[] from c
aa_mul(n,d,a,b) d[] = a[] * b[] multiply a[] and b[]
ac_mul(n,d,a,c) d[] = a[] * c multiply a[] and c
ca_mul(n,d,c,b) d[] = c * b[] multiply c and b[]
aa_div(n,d,a,b) d[] = a[] / b[] divide a[] by b[]
ac_div(n,d,a,c) d[] = a[] / c divide a[] by c
ca_div(n,d,c,b) d[] = c / b[] divide c by a[]
aa_mod(n,d,a,b) d[] = a[] % b[] Modulo a[] by b[]
ac_mod(n,d,a,c) d[] = a[] % c Modulo a[] by c
ca_mod(n,d,c,b) d[] = c % b[] Modulo c by a[]
aa_max(n,d,a,b) d[] = ARR_MAX(a[],b[]) max of a[] and b[]
ac_max(n,d,a,c) d[] = ARR_MAX(a[],c) max of a[] and c
ca_max(n,d,c,b) d[] = ARR_MAX(c,b[]) max of c and b[]
aa_min(n,d,a,b) d[] = ARR_MIN(a[],b[]) min of a[] and b[]
ac_min(n,d,a,c) d[] = ARR_MIN(a[],c) min of a[] and c
ca_min(n,d,c,b) d[] = ARR_MIN(c,b[]) min of c and b[]
aa_equ(n,d,a,b) d[] = a[] == b[] a[] equal b[]
ac_equ(n,d,a,c) d[] = a[] == c a[] equal c
ca_equ(n,d,c,b) d[] = c == b[] c equal b[]
aa_geq(n,d,a,b) d[] = a[] >= b[] a[] greater than or equal b[]
ac_geq(n,d,a,c) d[] = a[] >= c a[] greater than or equal c
ca_geq(n,d,c,b) d[] = c >= b[] c greater than or equal b[]
aa_gtr(n,d,a,b) d[] = a[] > b[] a[] greater than b[]
ac_gtr(n,d,a,c) d[] = a[] > c a[] greater than c
ca_gtr(n,d,c,b) d[] = c > b[] c greater than b[]
aa_leq(n,d,a,b) d[] = a[] <= b[] a[] less than or equal b[]
ac_leq(n,d,a,c) d[] = a[] <= c a[] less than or equal c
ca_leq(n,d,c,b) d[] = c <= b[] c less than or equal b[]
aa_les(n,d,a,b) d[] = a[] < b[] a[] less than b[]
ac_les(n,d,a,c) d[] = a[] < c a[] less than c
ca_les(n,d,c,b) d[] = c < b[] c less than b[]
aa_fun(n,d,a,b,f()) call f on a[] and b[]
d[] = f( a[] , b[] )
ac_fun(n,d,a,c,f()) call f on a[] and c
d[] = f( a[] , c )
ca_fun(n,d,c,b,f()) call f on c and a[]
d[] = f( c , a[] )
aa_t_fun(n,d,a,b,f(),T) call f on a[] and b[]
d[] = f((T)a[],(T)b[]) forcing a[i],b[i] to type T
ac_t_fun(n,d,a,c,f(),T) call f on a[] and c
d[] = f( (T)a[], (T)c ) forcing a[i],c to type T
ca_t_fun(n,d,c,b,f(),T) call f on c and a[]
d[] = f( (T)c, (T)a[] ) forcing c,a[i] to type T
UNARY OPERATIONS:
a_cpy(n,d,a) d[] = a[]; copy a to d
a_neg(n,d,a) d[] = - a[]; negate a to d
a_abs(n,d,a) d[] = ARR_ABS(a[]) absolute value of a to d
a_scale(n,d,a,s,o) scale & offset of a to d
d[] = o + s*a[] => linear poly of a to d
a_rsum(n,d,a) d[i] = d[i-1] + a[i] running sum of a to d
a_rprod(n,d,a) d[i] = d[i-1] * a[i] running sum of a to d
a_rmin(n,d,a) d[i] = ARR_MIN(d[i-1],d[i])
running minimum of a to d
a_rmax(n,d,a) d[i] = ARR_MAX(d[i-1],d[i])
running maximum of a to d
a_fun(n,d,a,f()) call function f on a to d
d[i] = f ( a[i] )
a_t_fun(n,d,a,f(),T) call function f on a to d
d[i] = f ( (T)a[i] ) forcing a to type T
FILL OPERATIONS:
a_index(n,d) d[i] = i index
a_indoff(n,d,o) d[i] = o + i index + offset
a_assign(n,d,c) d[] = c assign constant to d[]
a_i_scale(n,d,s,o) scale with index
d[i] = o + i*s linear poly on index
a_fill(n,d,b,e) d[i] = b + i*((e-b)/n) exclusive fill
d[0] = b, d[n] would be e
a_ifill(n,d,b,e) d[i] = b + i*((e-b)/(n-1) inclusive fill
d[0] = b, d[n-1] = e
a_t_fill(n,d,b,e,T) typed exclusive fill
d[i] = b + (T)i*(((T)(e-b))/(n))
a_t_ifill(n,d,b,e,T) typed inclusive fill
d[i] = b + (T)i*(((T)(e-b))/(n-1))
a_f_fun(n,d,f()) d[] = f() fill with return of function
a_i_fun(n,d,f()) d[i] = f(i) function fill with index parm
a_t_i_fun(n,d,f(),T) d[i] = f( (T)i ) typed function fill, type index
Note:
the various fill behave somewhat non-intuitivally due to using
integers as index. The following examples should clear up the
differences. f is a float destination array, i is the index, b = 1,
and e = 10
f(x)
call: rval:i=> 0 1 3 5 8 9
a_fill(10,f,0,10) => 0 1.000000 3.000000 5.000000 8.000000 9
a_ifill(10,f,0,10) => 0 1.000000 3.000000 5.000000 8.000000 10
a_t_fill(10,f,0,10,float) 0 1.000000 3.000000 5.000000 8.000000 9
a_t_ifill(10,f,0,10,float) 0 1.111111 3.333333 5.555555 8.888889 10
a_fill(10,f,0.0,10.0) 0 1.000000 3.000000 5.000000 8.000000 9
a_ifill(10,f,0.0,10.0) 0 1.111111 3.333333 5.555555 8.888889 10
What this all means is two things:
1> if you do not type the fill to a float or use a float b or e parameter,
the numbers you get back may not be what you expect.
2> The difference between fill and ifill is whether or not the last
index is going to be exactly the end point or not:
d[n-1] = b + (n-1)*(e-b)/(n-1) => e in ifill
d[n-1] = b + (n-1)*(e-b)/n => e - (e-b)/n in fill.
INFORMATION OPERATIONS
a_sum(n,d,a) d = a[+] sum of array
a_prod(n,d,a) d = a[*] product of array
a_max(n,d,a) d = max(a[]) max value in array
a_min(n,d,a) d = min(a[]) min value in array
a_minmax(n,l,u,a) min and max of array
l = min(a[]), u = max(a[])
Thats all the functions for now. If you have any questions, or would
like to register, please feel free to call or write me.
James P. Cruse
919 Capitola Ave #41
Capitola, CA 95010
(408) 475-7444 (h,m)